/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 14:44:45
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.bp.bean.BpMainProductEditBean;
import com.joysuch.wwyt.bp.bean.BpMainProductListBean;
import com.joysuch.wwyt.bp.bean.BpMainProductListBean.Search;
import com.joysuch.wwyt.bp.service.BpMainProductService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "主要产品")
@RequestMapping("/bp/main-product")
public class BpMainProductController {

    private static final Logger logger = LoggerFactory.getLogger(BpMainProductController.class);

    @Autowired
    private BpMainProductService bpMainProductService;

    @PostMapping("add")
    public Object add(@RequestBody @Valid BpMainProductEditBean entity) {
        try {
            bpMainProductService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    @PostMapping("update")
    public Object update(@RequestBody @Valid BpMainProductEditBean entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpMainProductService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, "更新失败");
        }
    }

    @GetMapping("/page")
    public Object page(Integer page, Integer size, Search condition) {
        try {
            QPageRequest request = QPageRequest.of(page, size);
            Pageable pageable = PageDataUtils.addOrderByDesc(request, "id");
            Page<BpMainProductListBean> data = bpMainProductService.findByPage(pageable, condition);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    public Object delete(Long id) {
        try {
            bpMainProductService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        try {
            return ResultBean.success(bpMainProductService.getById(id));
        } catch (Exception e) {
            logger.error("查询详情失败", e);
            return ResultBean.fail(101, "查询详情失败:" + e.getMessage());
        }
    }
}
